package com.lmb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmb.bean.AddressBook;
import com.lmb.dao.AddressBookDao;
import com.lmb.exception.CustomException;
import com.lmb.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookDao, AddressBook> implements AddressBookService {
    @Autowired
    private AddressBookDao addressBookDao;

    //修改地址
    @Transactional
    @Override
    public void updateAddressBook(AddressBook addressBook) {
        //判断地址是否已经存在
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getConsignee,addressBook.getConsignee());
        wrapper.eq(AddressBook::getPhone,addressBook.getPhone());
        wrapper.eq(AddressBook::getDetail,addressBook.getDetail());
        wrapper.eq(AddressBook::getUserId,addressBook.getUserId());
        wrapper.ne(AddressBook::getId,addressBook.getId());
        Integer count = addressBookDao.selectCount(wrapper);
        if (count>0){
            throw new CustomException(0,"该地址已存在！");
        }
        //修改
        addressBookDao.updateById(addressBook);
    }

    //删除地址
    @Transactional
    @Override
    public void deleteById(Long id) {
        //1.判断id不为null
        if(id==null){
            throw  new  CustomException(0,"id不能为空！");
        }
        addressBookDao.deleteById(id);
    }

}
